import Snake from './Snake'
import Food from './Food'
import ScorePanel from './ScorePanel'
class GameControl {
  snake: Snake
  food: Food
  scorePanel: ScorePanel
  direction: string = 'Right'
  // 创建一个属性用来记录游戏是否结束
  isLive = true

  constructor() {
    this.snake = new Snake()
    this.food = new Food()
    this.scorePanel = new ScorePanel(10, 10)
    this.init()
  }
  init() {
    document.addEventListener('keydown', this.keydownHandler.bind(this))
    this.run()
  }
  keydownHandler(event: KeyboardEvent) {
    this.direction = event.key
  }
  run() {
    let X = this.snake.X
    let Y = this.snake.Y
    switch (this.direction) {
      case "ArrowUp":
      case "Up":
        // 向上移动 top 减少
        Y -= 10;
        break;
      case "ArrowDown":
      case "Down":
        // 向下移动 top 增加
        Y += 10;
        break;
      case "ArrowLeft":
      case "Left":
        // 向左移动 left 减少
        X -= 10;
        break;
      case "ArrowRight":
      case "Right":
        // 向右移动 left 增加
        X += 10;
        break;
    }

    this.checkEat(X, Y);

    try {
      this.snake.X = X
      this.snake.Y = Y
    } catch (e) {
      alert(e.message)
      this.isLive = false
    }
    this.isLive && setTimeout(this.run.bind(this), 300 - (this.scorePanel.level - 1) * 30);
    // setTimeout(this.run.bind(this),300)
  }
  checkEat(X: number, Y: number) {

    if (X == this.food.X && Y == this.food.Y) {
      this.food.change()
      this.scorePanel.addScore()
      this.snake.addBody();
    }
  }
}
export default GameControl
